aux_source_directory(. SRC)


option(ENABLE_HIREDIS "Use Hireids test" ON)
if (ENABLE_HIREDIS)
    find_package(PkgConfig)
    if (${PKG_CONFIG_FOUND})
        pkg_check_modules(HIREDIS IMPORTED_TARGET hiredis)
    else ()
        set(ENABLE_HIREDIS OFF)
    endif ()
endif ()

option(ENABLE_GTEST "Use Google Test" ON)
if (ENABLE_GTEST)
    include(GoogleTest)
    find_package(GTest REQUIRED)
    add_library(gtest_compile_flags INTERFACE)
    target_compile_features(gtest_compile_flags INTERFACE cxx_std_17)
endif (ENABLE_GTEST)


foreach(FILE_PATH ${SRC})
    string(REGEX REPLACE ".+/(.+)\\..*" "\\1" FILE_NAME ${FILE_PATH})
    list(FIND NOT_COMPILE ${FILE_NAME} RESULT)
    if(${RESULT} EQUAL -1)
        if (${FILE_NAME} STREQUAL "test_redis")
            if (ENABLE_HIREDIS)
                add_executable(${FILE_NAME} ${FILE_PATH})
                target_link_libraries(${FILE_NAME} 
                    PRIVATE ZeppReactor 
                    PRIVATE PkgConfig::HIREDIS)
            else ()
                list(APPEND NOT_COMPILE ${FILE_NAME})
            endif ()
        elseif (${FILE_NAME} MATCHES "^gtest_") # gtest_ 前缀
            if (ENABLE_GTEST)
                add_executable(${FILE_NAME} ${FILE_PATH})
                target_link_libraries(${FILE_NAME} 
                    PRIVATE ZeppReactor 
                    gtest_compile_flags
                    PRIVATE GTest::gtest_main
                )
                gtest_discover_tests(${FILE_NAME}
                    TEST_PREFIX "gtest_")
            else ()
                list(APPEND NOT_COMPILE ${FILE_NAME})
            endif()
        else ()
            add_executable(${FILE_NAME} ${FILE_PATH})
            target_link_libraries(${FILE_NAME} PRIVATE ZeppReactor)
        endif ()
    endif()
endforeach()
